<?php


namespace App;

use Illuminate\Foundation\Auth\Type as Authenticatable;

namespace App;
use Session;

use Illuminate\Database\Eloquent\Model;
use DB;
class Goods_Home extends Model
{

	public $timestamps = false;
	public $table='goods';

	//获取商品信息
	public function getGoodsData($goods_id){
		return DB::table('goods')->where('is_on_sale',1)->where('goods_id',$goods_id)->get();
	}

	//获取商品的规格参数
	public function getGoodsSpec($goods_id){
		return DB::table('goods_attr')->where('attr_type',1)->where('goods_id',$goods_id)->get();	
	}

	//获取默认的规格
	public function getAutoOne($goods_id){
		// print_r($goods_id);die;
		return DB::table('product')->where('goods_id',$goods_id)->first();
	}



	//检测所选的属性下是否有可选属性 更新价钱
	public function checkattr($goods_id,$product_attr){
		return  DB::table('product')->where('goods_id',$goods_id)->where('product_number','>',0)->where('product_attr','like','%'.$product_attr.'%')->get(['product_attr']);
	}

//判断当前用户选择的是否有规格 然后判断当前选择的规格库存是否足够
	public function checkNumber($goods_id,$product_attr,$buy_nums){

		return DB::table('product')->where('goods_id',$goods_id)->where('product_attr',$product_attr)->where('product_number','>=',$buy_nums)->get();
	}

	// 判断当前选择的商品库存是否足够
	public function checkGoodsNumber($goods_id,$buy_nums){
		return DB::table('goods')->where('goods_id',$goods_id)->where('goods_number','>=',$buy_nums)->get();
	}

	//判断当前用户  当前属性规格添加到购物车了没
	public function checkUserAttr($uid,$goods_id,$product_attr){
		return  DB::table('cart')->where('uid',$uid)->where('goods_id',$goods_id)->where('product_attr_id',$product_attr)->get();

	}
	//添加购物车
	public function addOneCart($cartData){
		// print_r($cartData);die;
		return  DB::table('cart')->insert($cartData);

	}

	//商品跟据商品类型查找对应商品
	public function lists($cate_id){
		return self::where('cate_id',$cate_id)->paginate(20);
	}
	//商品类型列表
	public function listss(){
		return self::paginate(20);
	}
	//类型下的热销商品
	public function list_hot(){
		return self::where('is_hot',1)->where('is_on_sale',1)->orderBy('sort','desc')->paginate(20);
	}
	//类型下的热销商品
	public function list_new(){
		return self::where('is_new',1)->where('is_on_sale',1)->orderBy('sort','desc')->paginate(20);
	}

}